library(tidyverse)

theme_set(theme_bw())

load("data/processed/pwdprotect_effect_on_primary.RData")

variables_to_visualise <-
  # `new label` = "old label"
  c(`sex` = "sex", 
    `party logo in picture` = "logo", 
    `screen poistion (normalized)` = "screenPosition.unit", 
    `age (district centered)` = "age.district.centered",
    `Meetup eigenvector centr.` = "meetup_eigenvector_centrality",
    `Meetup degree centr.` = "meetup_degree_centrality",
    `Facebook outdegree centr.` = "facebook_out_degree_centrality",
    `Facebook degree centr.` = "facebook_degree_centrality",
    `candidate in local election` = "already_candidate",
    `same network community as leader` = "communities_usergiant_leader",
    `Forum degree centr.` = "forum_degree_centrality",
    `page rank in cross-platform network` = "page_rank_usergiant",
    `Forum outdegree centr.` = "forum_out_degree_centrality",
    `Forum eigenvector centr.` = "forum_eigenvector_centrality",
    `% female candidates in district` = "f.perc"
  )

map(c('output/figures/figure_5.eps', 'output/figures/figure_5.png'), 
    ~ ggsave(.x, plot =
               
               importance_usergiant[importance_usergiant$variable %in% variables_to_visualise,] %>% 
               
               # Uncomment to filter/recode variables name are selecting them in 
               # `variables_to_visualise`
               dplyr::filter(variable %in%
                               variables_to_visualise) %>%
               dplyr::mutate(variable = fct_recode(variable, !!!variables_to_visualise)) %>%
               
               ggplot(aes(y = fct_reorder(variable, `%IncMSE`, mean), x =`%IncMSE`)) +
               geom_boxplot() +
               theme(
                 legend.position = "none"
               ) +
               labs(x = 'percent increment in MSE', y = '', title = 'Feature importance') +
               theme_bw(),
             
             width = 20, height = 12, units = 'cm'
    )
)